using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using BUS;
using DTO;
using System.Resources;
using System.Globalization;
namespace QuanLyPhongMach
{
    public partial class QuanLyThuocDichVu : DevComponents.DotNetBar.Metro.MetroForm
    {
        public QuanLyThuocDichVu()
        {
            InitializeComponent();
            DanhSachThuoc();
            DanhSachDichVu();
        }

        private void DanhSachThuoc()
        {
            DataTable dt = new DataTable();
            dt = Thuoc_BUS.DanhSachThuoc();
            dgvThuoc.DataSource = dt;
        }

        string ThemThanhCong, ThemThatBai;
        string XoaThanhCong, XoaThatBai;
        string CapNhatThanhCong, CapNhatThatBai;
        string BanCoThatSuMuonXoa, ThongBao;

        private void bbThemThuoc_Click(object sender, ClickEventArgs e)
        {
            try
            {
                string[] arrngay = (dtiNgayHetHan.Text).Split('/');
                if (arrngay.Length == 1)
                {
                    MessageBox.Show("Vui lòng nhập ngày");
                }
                else
                {
                    Thuoc_DTO thuoc_dto = new Thuoc_DTO();
                    thuoc_dto.TenThuoc = txtTenThuoc.Text;
                    if (txtGia.Text == "")
                    {
                        thuoc_dto.Gia = 0;
                    }
                    else
                    {
                        thuoc_dto.Gia = int.Parse(txtGia.Text);
                    }
                    thuoc_dto.TinhTrang = txtDang.Text;
                    thuoc_dto.NgayHetHan = dtiNgayHetHan.Text;
                    //xử lý ngày
                    string ngayformat = arrngay[1] + "/" + arrngay[0] + "/" + arrngay[2];
                    thuoc_dto.NgayHetHan = ngayformat;

                    if (Thuoc_BUS.ThemThuoc(thuoc_dto) == true)
                    {
                        MessageBox.Show(ThemThanhCong);
                        DanhSachThuoc();
                    }
                    else
                    {
                        MessageBox.Show(ThemThatBai);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void bbXoaThuoc_Click(object sender, ClickEventArgs e)
        {

            
            if (txtMaThuoc.Text == "")
            {
                MessageBox.Show("Vui lòng nhập mã");
            }
            else
            {
                if (MessageBox.Show(BanCoThatSuMuonXoa, ThongBao, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    string ma = dgvThuoc.CurrentRow.Cells[1].Value.ToString();
                    if (Thuoc_BUS.XoaThuoc(ma) == true)
                    {
                        MessageBox.Show(XoaThanhCong);
                        txtMaThuoc.Text = "";
                        txtGia.Text = "";
                        txtDang.Text = "";
                        txtTenThuoc.Text = "";
                        dtiNgayHetHan.Text = "";
                        DanhSachThuoc();
                    }
                    else
                    {
                        MessageBox.Show(XoaThatBai);
                    }
                }
            }
        }


        private void bbSuaThuoc_Click(object sender, ClickEventArgs e)
        {
            try
            {
                if (txtMaThuoc.Text == "")
                {
                    MessageBox.Show("Vui lòng nhập mã");
                }
                else
                {
                    Thuoc_DTO thuoc_dto = new Thuoc_DTO();
                    thuoc_dto.MaThuoc = txtMaThuoc.Text;
                    thuoc_dto.TenThuoc = txtTenThuoc.Text;
                    thuoc_dto.TinhTrang = txtDang.Text;
                    //xử lý ngày
                    string[] arrngay = (dtiNgayHetHan.Text).Split('/');
                    string ngayformat = arrngay[1] + "/" + arrngay[0] + "/" + arrngay[2];
                    thuoc_dto.NgayHetHan = ngayformat;
                    thuoc_dto.Gia = float.Parse(txtGia.Text);
                    if (Thuoc_BUS.CapNhatThuoc(thuoc_dto) == true)
                    {
                        MessageBox.Show(CapNhatThanhCong);
                        DanhSachThuoc();
                    }
                    else
                    {
                        MessageBox.Show(CapNhatThatBai);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }



        private void dgvThuoc_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            txtMaThuoc.Text = dgvThuoc.CurrentRow.Cells[1].Value.ToString();
            txtTenThuoc.Text = dgvThuoc.CurrentRow.Cells[2].Value.ToString();
            float GiaThuoc = float.Parse(dgvThuoc.CurrentRow.Cells[4].Value.ToString());
            int t = Convert.ToInt32(GiaThuoc);
            txtGia.Text = t.ToString();           
            txtDang.Text = dgvThuoc.CurrentRow.Cells[3].Value.ToString();
            dtiNgayHetHan.Text = dgvThuoc.CurrentRow.Cells[5].Value.ToString();
        }


        private void DanhSachDichVu()
        {
            DataTable dt = new DataTable();
            dt = DichVu_BUS.DanhSachDichVu();
            dgvDichVu.DataSource = dt;
        }
        private void bbThemDichVu_Click(object sender, ClickEventArgs e)
        {
            try
            {
                if (txtTenDichVu.Text == "")
                {
                    MessageBox.Show("Vui lòng nhập tên dịch vụ");
                }
                else
                {
                    DichVu_DTO dv_dto = new DichVu_DTO();
                    dv_dto.TenDichVu = txtTenDichVu.Text;
                    if (txtGiaDichVu.Text == "")
                    {
                        dv_dto.Gia = 0;
                    }
                    else
                    {
                        dv_dto.Gia = float.Parse(txtGiaDichVu.Text);
                    }
                
                    if (DichVu_BUS.ThemDichVu(dv_dto) == true)
                    {
                        MessageBox.Show(ThemThanhCong);
                        DanhSachDichVu();
                    }
                    else
                    {
                        MessageBox.Show(ThemThatBai);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }

        private void bbXoaDichVu_Click(object sender, ClickEventArgs e)
        {

            try
            {
                if (MessageBox.Show("Bạn thực sự muốn xóa không", "Thông Báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    string ma = txtMaDichVu.Text;
                    if (DichVu_BUS.XoaDichVu(ma) == true)
                    {
                        MessageBox.Show(XoaThanhCong);
                        txtMaDichVu.Text = "";
                        txtTenDichVu.Text = "";
                        txtGiaDichVu.Text = "";
                        DanhSachDichVu();
                    }
                    else
                    {
                        MessageBox.Show(XoaThatBai);
                    }
                }
            }
            catch (Exception ex)
            {
 
            }
            
        }

        private void bbSuaDV_Click(object sender, ClickEventArgs e)
        {
            if (txtMaDichVu.Text == "")
            {
                MessageBox.Show("Vui lòng nhập mã");
            }
            else
            {
                DichVu_DTO dv_dto = new DichVu_DTO();
                dv_dto.MaDV = txtMaDichVu.Text;
                dv_dto.TenDichVu = txtTenDichVu.Text;
                dv_dto.Gia = float.Parse(txtGiaDichVu.Text);
                if (DichVu_BUS.CapNhatDichVu(dv_dto) == true)
                {
                    MessageBox.Show(CapNhatThanhCong);
                    DanhSachDichVu();
                }
                else
                {
                    MessageBox.Show(CapNhatThatBai);
                }
            }
        }

        private void dgvDichVu_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            txtMaDichVu.Text = dgvDichVu.CurrentRow.Cells[1].Value.ToString();
            txtTenDichVu.Text = dgvDichVu.CurrentRow.Cells[2].Value.ToString();
            txtGiaDichVu.Text = dgvDichVu.CurrentRow.Cells[3].Value.ToString();
        }

        private void dgvDichVu_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
        {
            dgvDichVu.Rows[e.RowIndex].Cells["STTDichVu"].Value = e.RowIndex + 1;
        }

        private void dgvThuoc_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
        {
            dgvThuoc.Rows[e.RowIndex].Cells["STTThuoc"].Value = e.RowIndex + 1;
        }

        private void bbLamMoiDichVu_Click(object sender, ClickEventArgs e)
        {
            txtMaDichVu.Text = "";
            txtTenDichVu.Text = "";
            txtGiaDichVu.Text = "";
        }

        private void bbLamMoiTHuoc_Click(object sender, ClickEventArgs e)
        {
            sVDichVu.Enabled = false ;
            txtMaThuoc.Text = "";
            txtTenThuoc.Text = "";
            txtGia.Text = "";
            dtiNgayHetHan.Text = "";
            txtDang.Text = "";
        }

        private void QuanLyThuoc_FormClosing(object sender, FormClosingEventArgs e)
        {
            sVDichVu.Enabled = false;
          
        }

        private void txtGia_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
                e.Handled = true;
        }

        private void txtGiaDichVu_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
                e.Handled = true;
        }

        private void superTabItem3_Click(object sender, EventArgs e)
        {
            sVDichVu.Enabled = false;
        }

        private void superTabItem2_Click(object sender, EventArgs e)
        {
            sVDichVu.Enabled = false;
        }

        CultureInfo culture;
        private void SetLanguage(string cultureName)
        {
            culture = CultureInfo.CreateSpecificCulture(cultureName);
            ResourceManager rm = new
                ResourceManager("QuanLyPhongMach.Language.MyResource", typeof(PhongMach).Assembly);
            superTabItem2.Text = rm.GetString("Drug", culture);
            superTabItem3.Text = rm.GetString("Services", culture);
            groupBox4.Text = rm.GetString("Manage Drug", culture);
            groupBox1.Text = rm.GetString("Manage Services", culture);
            STTDichVu.HeaderText = rm.GetString("No.", culture);
            STTThuoc.HeaderText = rm.GetString("No.", culture);
            lblMaDichVu.Text = rm.GetString("Service ID", culture);
            lblTenDV.Text = rm.GetString("Service Name", culture);
            lblGiaDV.Text = rm.GetString("Service Price", culture);
            lblMaThuoc.Text = rm.GetString("Drug ID", culture);
            lblTenThuoc.Text = rm.GetString("Drug Name", culture);
            lblGiaThuoc.Text = rm.GetString("Drug Price", culture);
            lblNgayHetHan.Text = rm.GetString("Expiration date", culture);
            lblDang.Text = rm.GetString("Type", culture);
            ThemThanhCong = rm.GetString("Insert successfully", culture);
            ThemThatBai = rm.GetString("Insert fail", culture);
            XoaThanhCong = rm.GetString("Delete successfully", culture);
            XoaThatBai = rm.GetString("Delete fail", culture);
            CapNhatThanhCong = rm.GetString("Update successfully", culture);
            CapNhatThatBai = rm.GetString("Update fail", culture);
            BanCoThatSuMuonXoa = rm.GetString("Do you really want to delete ?", culture);
            ThongBao = rm.GetString("Announcement", culture);
            bbLamMoiTHuoc.TooltipText = rm.GetString("Refresh", culture);
            bbLamMoiDichVu.TooltipText = rm.GetString("Refresh", culture);
            bbThemDichVu.TooltipText = rm.GetString("Service insert", culture);
            bbXoaDichVu.TooltipText = rm.GetString("Service delete", culture);
            bbSuaDV.TooltipText = rm.GetString("Service update", culture);
            bbThemThuoc.TooltipText = rm.GetString("Drug insert", culture);
            bbXoaThuoc.TooltipText = rm.GetString("Drug delete", culture);
            bbSuaThuoc.TooltipText = rm.GetString("Drug update", culture);
            TenDV.HeaderText = rm.GetString("Service Name", culture);
            MaDV.HeaderText = rm.GetString("Service Price", culture);
            GiaDichVu.HeaderText = rm.GetString("Drug Price", culture);
            MaThuoc.HeaderText = rm.GetString("Drug ID", culture);
            TenThuoc.HeaderText = rm.GetString("Drug Name", culture);

        }

        private void QuanLyThuocDichVu_Load(object sender, EventArgs e)
        {
            if (PhongMach.bLanguage == true)//tiếng việt
            {
                SetLanguage("vi-VN");
            }
            else
            {
                SetLanguage("en-US");
            }
        }

    }
}